/**************************************************************************
 * @file usr_uart.h
 * @author 黑马小乌龟 (532352406@qq.com)
 * @brief 
 * @version 0.1
 * @date 2025-10-09
 * 
 * @copyright Copyright (c) 2025
 * 
 *************************************************************************/
#ifndef USR_UART__H_H
#define USR_UART__H_H

#ifdef __cplusplus
extern "C"{
#endif
#include<stdint.h>
#include<stdio.h>


#include "hc32_ll.h"
#include "ev_hc32f334_lqfp64_bsp.h"
/*******************************************************************************
 * Local type definitions ('typedef')
 ******************************************************************************/

/*******************************************************************************
 * Local pre-processor symbols/macros ('#define')
 ******************************************************************************/
/* Peripheral register WE/WP selection */
#define LL_PERIPH_SEL_USR_UART                   (LL_PERIPH_GPIO | LL_PERIPH_FCG | LL_PERIPH_PWC_CLK_RMU | \
                                         LL_PERIPH_EFM)

/* DMA definition */
#define RX_DMA_UNIT                     (CM_DMA)
#define RX_DMA_CH                       (DMA_CH4)
#define RX_DMA_FCG_ENABLE()             (FCG_Fcg0PeriphClockCmd(FCG0_PERIPH_DMA, ENABLE))
#define RX_DMA_TRIG_SEL                 (AOS_DMA_4)
#define RX_DMA_TRIG_EVT_SRC             (EVT_SRC_USART2_RI)
#define RX_DMA_RECONF_TRIG_SEL          (AOS_DMA_RC)
#define RX_DMA_RECONF_TRIG_EVT_SRC      (EVT_SRC_AOS_STRG)
#define RX_DMA_TC_INT                   (DMA_INT_TC_CH4)
#define RX_DMA_TC_FLAG                  (DMA_FLAG_TC_CH4)
#define RX_DMA_TC_IRQn                  (INT005_IRQn)
#define RX_DMA_TC_INT_SRC               (INT_SRC_DMA_TC4)

#define TX_DMA_UNIT                     (CM_DMA)
#define TX_DMA_CH                       (DMA_CH3)
#define TX_DMA_FCG_ENABLE()             (FCG_Fcg0PeriphClockCmd(FCG0_PERIPH_DMA, ENABLE))
#define TX_DMA_TRIG_SEL                 (AOS_DMA_3)
#define TX_DMA_TRIG_EVT_SRC             (EVT_SRC_USART2_TI)
#define TX_DMA_TC_INT                   (DMA_INT_TC_CH3)
#define TX_DMA_TC_FLAG                  (DMA_FLAG_TC_CH3)
#define TX_DMA_TC_IRQn                  (INT009_IRQn)
#define TX_DMA_TC_INT_SRC               (INT_SRC_DMA_TC3)

/* Timer0 unit & channel definition */
#define TMR0_UNIT                       (CM_TMR0_1)
#define TMR0_CH                         (TMR0_CH_B)
#define TMR0_FCG_ENABLE()               (FCG_Fcg2PeriphClockCmd(FCG2_PERIPH_TMR0_1, ENABLE))

/* USART RX/TX pin definition */
#define USART_RX_PORT                   (GPIO_PORT_B)   /* PC4: USART2_RX */
#define USART_RX_PIN                    (GPIO_PIN_04)
#define USART_RX_GPIO_FUNC              (GPIO_FUNC_37)

#define USART_TX_PORT                   (GPIO_PORT_B)   /* PC10: USART2_TX */
#define USART_TX_PIN                    (GPIO_PIN_03)
#define USART_TX_GPIO_FUNC              (GPIO_FUNC_36)

/* USART unit definition */
#define USART_UNIT                      (CM_USART2)
#define USART_FCG_ENABLE()              (FCG_Fcg3PeriphClockCmd(FCG3_PERIPH_USART2, ENABLE))

/* USART baudrate definition */
#define USART_BAUDRATE                  (1000000ul)//(115200UL)

/* USART timeout bits definition */
#define USART_TIMEOUT_BITS              (2000U)

/* USART interrupt definition */
#define USART_TX_CPLT_IRQn              (INT003_IRQn)
#define USART_TX_CPLT_INT_SRC           (INT_SRC_USART2_TCI)

#define USART_RX_ERR_IRQn               (INT007_IRQn)
#define USART_RX_ERR_INT_SRC            (INT_SRC_USART2_EI)

#define USART_RX_TIMEOUT_IRQn           (INT008_IRQn)
#define USART_RX_TIMEOUT_INT_SRC        (INT_SRC_USART2_RTO)

/* Application frame length max definition */
#define APP_FRAME_LEN_MAX     (128U)
#define APP_CMD_FRAME_LEN_MAX (APP_FRAME_LEN_MAX)

#define OUTPUT_VOLTAGE_CMD "vo"
#define INPUT_VOLTAGE_CMD  "vi"
#define OUTPUT_CURRENT_CMD "io"
#define OUTPUT_POWER_CMD   "po"
#define OUTPUT_STATE_CMD   "os"
#define INPUT_STATE_CMD    "is"
#define SET_OUT_VOLTAGE    "sv"
#define SET_OUT_CURRENT    "si"
#define SET_ACTIVE_INFOR   "ca"
#define BOARD_TEMPERATUR   "tmp"
#define BUCK_BOOST_MCU_VER "bmcu"
#define PD_MCU_VERSION     "pmcu"
#define HOST_SYNC_DAT_CMD  "sync"

int32_t usr_uart_init(void);
int usr_uart_send(char *p_buf, uint16_t len);
uint8_t *usr_uart_get_msg_pkg_handle(void);
void usr_uart_msg_process(void);
int usr_uart_send_a_msg(char *cmd_str,char *param_str);
void usr_uart_msg_pool_handle(void);
uint8_t usr_uart_get_actiove_msg_num_in_pool(void);

#ifdef __cplusplus
}
#endif

#endif //USR_UART__H_H


